package com.swak.vertx.transport.vertx.btc;

import io.vertx.core.Handler;
import io.vertx.core.VertxException;
import io.vertx.core.impl.btc.BlockedThreadEvent;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;

/**
 * 处理线程运行事件过长事件
 * 
 * @author 618lf
 */
public class BlockedThreadEventHandler implements Handler<BlockedThreadEvent> {

	private static final Logger log = LoggerFactory.getLogger(com.swak.reactivex.threads.BlockedThreadChecker.class);

	@Override
	public void handle(BlockedThreadEvent bte) {
		final String message = "Thread " + bte.thread() + " has been blocked for " + (bte.duration() / 1_000_000)
				+ " ms, time limit is " + (bte.maxExecTime() / 1_000_000) + " ms";
		if (bte.duration() <= bte.warningExceptionTime()) {
			log.debug(message);
		} else {
			VertxException stackTrace = new VertxException("Thread blocked");
			stackTrace.setStackTrace(bte.thread().getStackTrace());
			log.warn(message, stackTrace);
		}
	}
}
